package com.jshop.controller.admin;
import org.json.JSONException;

import com.jfinal.plugin.activerecord.Page;
import com.jshop.model.Brand;
import com.jshop.model.Rule;
import com.jshop.model.Ruledata;
import com.jshop.utils.DatagridUtil;
/**
 * 后台类 - 商品分类
 * 
 */
public class RuleController extends BaseController {
	public void list(){
		render("list.html");
	}
	public void datagrid(){
		Integer page = getParaToInt("page",1);
		Integer rows = getParaToInt("rows",10);
		String sql="from rule";
		if(getPara("rule_name")!=null&&!getPara("rule_name").equals("")){
			sql+=" where rule_name='"+getPara("rule_name")+"'";
		}
		sql+=" order by rule_id asc";
		Page<Rule> list=Rule.dao.paginate(page, rows, "select *",sql);
		renderJson(DatagridUtil.getList(list));
	}
	public void add() throws JSONException{
		if(isPost()){
			Rule dao=new Rule();
			boolean rs=dao.set("rule_name", getPara("rule_name"))
			.set("rule_memo",getPara("rule_memo"))
			.save();
			if(rs){
				ajaxJsonSuccessMessage("添加成功");
			}else{
				ajaxJsonErrorMessage("添加失败");
			}
		}else{
			render("add.html");
		}
		
	}
	public void edit() throws JSONException{
		if(isPost()){
			Rule dao=new Rule();
			boolean rs=dao.set("rule_name", getPara("rule_name"))
			.set("rule_id",getPara("rule_id"))
			.set("rule_memo",getPara("rule_memo"))
			.update();
			if(rs){
				ajaxJsonSuccessMessage("更新成功");
			}else{
				ajaxJsonErrorMessage("更新失败");
			}
		}else{
			setAttr("info", Rule.dao.findById(getParaToInt("id")));
			render("edit.html");
		}
	}
	public void remove() throws JSONException{
		int id=getParaToInt("id");
		boolean rs=Rule.dao.deleteById(id);
		if(rs){
			ajaxJsonSuccessMessage("删除成功");
		}else{
			ajaxJsonErrorMessage("删除失败");
		}
	}
	
	public void addruledata() throws JSONException{
		if(isPost()){
			Ruledata dao=new Ruledata();
			boolean rs=dao.set("ruledata_name", getPara("ruledata_name"))
			.set("ruledata_logo", getPara("ruledata_logo"))
			.set("ruledata_order", getPara("ruledata_order"))
			.set("rule_id", getPara("rule_id"))
			.save();
			if(rs){
				ajaxJsonSuccessMessage("添加成功");
			}else{
				ajaxJsonErrorMessage("添加失败");
			}
		}else{
			int id=getParaToInt("id");
			setAttr("rule_id", id);
			render("addruledata.html");
		}
		
	}
	
	public void ruledata(){
		int id=getParaToInt("id");
		setAttr("rule_id", id);
		render("ruledata.html");
	}
	
	public void ruledatagrid(){
		Integer page = getParaToInt("page",1);
		Integer rows = getParaToInt("rows",10);
		String sql="from ruledata where rule_id="+getParaToInt("rule_id");
		Page<Ruledata> list=Ruledata.dao.paginate(page, rows, "select *",sql);
		renderJson(DatagridUtil.getList(list));
	}
	public void editruledata() throws JSONException{
		if(isPost()){
			Ruledata dao=new Ruledata();
			boolean rs=dao.set("ruledata_name", getPara("ruledata_name"))
			.set("ruledata_logo", getPara("ruledata_logo"))
			.set("ruledata_id", getPara("ruledata_id"))
			.set("ruledata_order", getPara("ruledata_order"))
			.set("rule_id", getPara("rule_id"))
			.update();
			if(rs){
				ajaxJsonSuccessMessage("更新成功");
			}else{
				ajaxJsonErrorMessage("更新失败");
			}
		}else{
			setAttr("info", Ruledata.dao.findById(getParaToInt("id")));
			render("editruledata.html");
		}
		
	}
	public void removeruledata() throws JSONException{
		int id=getParaToInt("id");
		boolean rs=Ruledata.dao.deleteById(id);
		if(rs){
			ajaxJsonSuccessMessage("删除成功");
		}else{
			ajaxJsonErrorMessage("删除失败");
		}
	}
}
